package com.kiln.juxingqs.ui

import android.view.View
import client.global.commlib.dialog.CameraOrSelectDialog
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdhyAdapter
import com.kiln.juxingqs.api.bean.MyMember
import com.kiln.juxingqs.databinding.ActivityWdhy2Binding
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.ui.vm.WdhyViewModel
import java.util.*

/**
 * 会员中心
 */
class WdhyActivity :
    BaseMiddleActivity<WdhyViewModel, ActivityWdhy2Binding>() {
    override fun getLayout(): Int {
        return R.layout.activity_wdhy2
    }

    val adapter: WdhyAdapter by lazy {
        WdhyAdapter(this@WdhyActivity)
    }

    override fun initViewModel(): WdhyViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.recyclerView.adapter = adapter
//        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
//        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
//        decoration.setDrawable(dividerDrawable)
//        binding.recyclerView.addItemDecoration(decoration)

        binding.apply {
            emptyLayout.setOnClickListener { viewModel.holdMyMember() }
//            refreshLayout.setOnRefreshListener {
//                adapter.hasMoreState.value = true
////                viewModel.pageOne.value = 0
//                viewModel.holdMyMember()
//            }
//            recyclerView.onLoadMore = {
////                adapter.hasMoreState.value!! &&
//                if (!viewModel.isOneLoading.value!!) {
//                    viewModel.holdMyMember()
//                }
//            }
        }
    }


    lateinit var userInfo: MyMember.MUserInfo

    override fun observerUI() {
        viewModel.callMyMember.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    userInfo = it.data?.user_info!!
                    userInfo?.let {
                        binding.userInfo = userInfo

                        mImgLoader.load(userInfo!!.avatar).error(R.mipmap.ic_launcher_round).into(binding.userAvatar)
                    }
                    val list =
                        it.data?.member_list as MutableList<MyMember.Member?>?
                    adapter.submitList(list)
                    adapter.hasMoreState.value = false
                    binding.emptyLayout.visibility = View.GONE

                } else {
//                    binding.emptyLayout.visibility = View.GONE
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

    }


    override fun initData() {
        viewModel.holdMyMember()

//        val list = ArrayList<String>()
//        list.add("")
//        list.add("")
//        list.add("")
//        list.add("")
//        list.add("")
//        adapter.submitList(list)
//        adapter.hasMoreState.value = false
    }

    fun imgSelect() {
        MsgTitleDialog(this).apply {
            okTxt = "去设置"
            cancleTxt = "取消"
            title = "设置用户头像？"
            ok = {
                CameraOrSelectDialog(this@WdhyActivity).apply {
                    suc = {
                        viewModel.userAvatar.value = it
                        viewModel.setAvatar(this@WdhyActivity)
                    }
                }.show()
            }
        }.show()
    }


}